package com.qeegoo.b2bautozityp.moudle.good.viewmodel;

import android.databinding.ObservableField;

import com.cropper.imagepicker.ImagePicker;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.model.ImageUploadBean;
import com.qeegoo.b2bautozityp.utils.ImgUtils;
import com.qeegoo.b2bautozityp.widget.ChoosePicDialog;

import java.io.File;

import base.lib.util.ActivityManager;

/**
 * 王少岩 在 2017/9/15 创建了它
 */

public class GoodsImgViewModel {
    public ObservableField<String> imgId = new ObservableField<>("");
    public ObservableField<String> imgUrl = new ObservableField<>("");
    public ObservableField<Boolean> showDel = new ObservableField<>(false);
    private ImagePicker mImagePicker;
    private RequestApi mRequestApi;
    private String mGoodsId;

    public GoodsImgViewModel(RequestApi requestApi, ImagePicker imagePicker) {
        mRequestApi = requestApi;
        mImagePicker = imagePicker;
    }

    public void setGoodsId(String goodsId) {
        mGoodsId = goodsId;
    }

    public ReplyCommand addCommand = new ReplyCommand(() ->
            new ChoosePicDialog(ActivityManager.getActivity(), mImagePicker, imageUri -> {
                File tmp = ImgUtils.getTempFile();
                ImgUtils.compressBmpToFile(ActivityManager.getActivity(), imageUri.getPath(), tmp);
                mRequestApi.imageUpload(HttpParams.uploadImage(mGoodsId, tmp))
                        .compose(RetrofitService.applySchedulers())
                        .map(new RetrofitService.HttpResultFunc<>())
                        .subscribe(new ProgressSubscriber<ImageUploadBean>() {
                            @Override
                            public void onNext(ImageUploadBean imageUploadBean) {
                                imgUrl.set(imageUploadBean.tempImagePath);
                                showDel.set(true);
                            }
                        });
            }).show()
    );

    public ReplyCommand delCommand = new ReplyCommand(() -> {
        imgUrl.set("");
        showDel.set(false);
//        mRequestApi.deleteImage(HttpParams.deleteImage(""))
//                .compose(RetrofitService.applySchedulers())
//                .subscribe(new ProgressSubscriber<HttpResult>() {
//                    @Override
//                    public void onNext(HttpResult httpResult) {
//                        ToastUtils.showToast(httpResult.getStatus().getMsg());
//                        if (httpResult.getStatus().isSuccess()) {
//                        }
//                    }
//                });
    });
}
